#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "myfile.tab.h"

extern int yyparse(void);
extern FILE* yyin;

// 测试用例结构
struct TestCase {
    const char* name;
    const char* input;
};

// 测试用例
struct TestCase test_cases[] = {
    {
        "Basic Block Test 1",
        "int i;\n"
        "int sum;\n"
        "i = 0;\n"
        "sum = 0;\n"
        "while (i < 10) {\n"
        "    sum = sum + i;\n"
        "    i = i + 1;\n"
        "}\n"
    },
    {
        "Basic Block Test 2",
        "int i;\n"
        "int j;\n"
        "int sum;\n"
        "i = 0;\n"
        "j = 0;\n"
        "sum = 0;\n"
        "while (i < 5) {\n"
        "    while (j < 5) {\n"
        "        sum = sum + 1;\n"
        "        j = j + 1;\n"
        "    }\n"
        "    i = i + 1;\n"
        "}\n"
    }
};

void run_test(const char* test_name, const char* input) {
    printf("\nRunning test: %s\n", test_name);
    printf("Input code:\n%s\n", input);
    
    // 创建临时输入文件
    FILE* temp = fopen("test_input.txt", "w");
    if (!temp) {
        printf("Error: Cannot create test input file\n");
        return;
    }
    fprintf(temp, "%s", input);
    fclose(temp);
    
    // 打开文件进行解析
    yyin = fopen("test_input.txt", "r");
    if (!yyin) {
        printf("Error: Cannot open test input file\n");
        return;
    }
    
    // 运行解析器
    printf("\nParser output:\n");
    yyparse();
    
    fclose(yyin);
    remove("test_input.txt");
}

int main() {
    printf("Starting compiler tests...\n");
    printf("=========================\n");
    
    int num_tests = sizeof(test_cases) / sizeof(test_cases[0]);
    for (int i = 0; i < num_tests; i++) {
        run_test(test_cases[i].name, test_cases[i].input);
        printf("\n--------------------------\n");
    }
    
    printf("\nAll tests completed.\n");
    return 0;
}